package cn.edu.hust.sse.software7.class3.DAO;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import cn.edu.hust.sse.software7.class3.common.PMF;
import cn.edu.hust.sse.software7.class3.entities.EBProduct;

public class EBProductDAO {
	private static final Logger log = Logger.getLogger(EBProductDAO.class
			.getName());

	public boolean isExistProduct(EBProduct prod) {
		log.info("check whether the prod " + prod.getProductName()
				+ " exist begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		boolean result;
		try {
			tx.begin();
			Query query = pm.newQuery(EBProduct.class);
			String filter = "productName == '" + prod.getProductName() + "'";
			if (null != prod.getMyCategoryId()) {
				filter += " && myCategoryId == " + prod.getMyCategoryId() + "";
			} else {
				filter += " && myCategoryId == null";
			}
			query.setFilter(filter);
			List<EBProduct> products = (List<EBProduct>) query.execute();
			if (products.isEmpty()) {
				log.finest("the prod " + prod.getProductName() + " isnot exist");
				result = false;
			} else {
				log.finest("the prod  " + prod.getProductName()
						+ "already exist.");
				result = true;
			}
			tx.commit();
		} catch (Exception e) {
			log.info("check prod exist occur exception!!!\n"
					+ e.getStackTrace());
			result = true;
		}
		return result;
	}

	public void addProduct(EBProduct product) {
		log.info("add product " + product.getProductName() + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			pm.makePersistent(product);
			tx.commit();
			log.finest("add product " + product.getProductName() + " success.");
		} catch (Exception e) {
			log.info("add product " + product.getProductName() + "failed!!!\n"
					+ e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	public void updateProduct(EBProduct product) {
		log.info("modify product " + product.getProductName() + " begin.");
		String productName = product.getProductName();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			EBProduct prod2update = pm.getObjectById(EBProduct.class,
					product.getProductId());
			prod2update.setProductImage(product.getProductImage());
			prod2update.setProductDescription(product.getProductDescription());
			prod2update.setMount(product.getMount());
			prod2update.setLowestPrice(product.getLowestPrice());
			prod2update.setHightestBid(product.getHightestBid());
			prod2update.setFreight(product.getFreight());
			pm.makePersistent(prod2update);
			tx.commit();
			log.finest("modify product " + productName + " success.");
		} catch (Exception e) {
			log.info("modify product " + productName + "failed!!!\n"
					+ e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	public void deleteProduct(EBProduct product) {
		log.info("delete product " + product.getProductName() + " begin.");
		String delName = product.getProductName();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			pm.deletePersistent(pm.getObjectById(EBProduct.class,
					product.getProductId()));
			tx.commit();
			log.finest("delete product " + delName + " success.");
		} catch (Exception e) {
			log.info("delete product " + delName + "failed!!!\n"
					+ e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	public void deleteAllProduct(List<EBProduct> products) {
		log.info("delete products " + products + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			pm.deletePersistentAll(products);
			tx.commit();
			log.finest("delete products  success.");
		} catch (Exception e) {
			log.info("delete products failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	public List<EBProduct> getProdsByCategory(Long categoryId) {
		log.info("get Prods By Category " + categoryId + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBProduct> prods = null;
		try {
			tx.begin();
			Query query = pm.newQuery(EBProduct.class);
			query.setFilter("myCategoryId == '" + categoryId + "'");
			prods = (List<EBProduct>) query.execute();
			tx.commit();
			log.finest("get Prods By Category " + categoryId + " finished.");
		} catch (Exception e) {
			log.info("get Prods By Category " + categoryId + " failed!!!\n"
					+ e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return prods;
	}

	public List<EBProduct> getProdsByProdName(String prodName) {
		log.info("get Prods By Prod Name " + prodName + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBProduct> prods = null;
		try {
			tx.begin();
			Query query = pm.newQuery(EBProduct.class);
			query.setFilter("productName == '" + prodName + "'");
			prods = (List<EBProduct>) query.execute();
			tx.commit();
			log.finest("get Prods By Prod Name " + prodName + " success.");
		} catch (Exception e) {
			log.info("get Prods By Prod Name " + prodName + " failed!!!\n"
					+ e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return prods;
	}

	public List<EBProduct> getProdsByLowestPriceScope(Float low, Float high) {
		log.info("get Prods By Lowest Price Scope from " + low + " to " + high
				+ " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBProduct> prods = null;
		try {
			tx.begin();
			Query query = pm.newQuery(EBProduct.class);
			query.setFilter("lowestPrice >= '" + low + "'");
			query.setFilter("lowestPrice <= '" + high + "'");
			prods = (List<EBProduct>) query.execute();
			tx.commit();
			log.finest("get Prods By Lowest Price Scope from " + low + " to "
					+ high + "  success.");
		} catch (Exception e) {
			log.info("get Prods By Lowest Price Scope from " + low + " to "
					+ high + "  failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return prods;
	}

	public List<EBProduct> getProdsByHightestBidPriceScope(Float low, Float high) {
		log.info("get Prods By HightestBid Price Scope from " + low + " to "
				+ high + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBProduct> prods = null;
		try {
			tx.begin();
			Query query = pm.newQuery(EBProduct.class);
			query.setFilter("hightestBid >= '" + low + "'");
			query.setFilter("hightestBid <= '" + high + "'");
			prods = (List<EBProduct>) query.execute();
			tx.commit();
			log.finest("get Prods By HightestBid Price Scope from " + low
					+ " to " + high + "   success.");
		} catch (Exception e) {
			log.info("get Prods By HightestBid Price Scope from " + low
					+ " to " + high + "  failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return prods;
	}

	public EBProduct getProdById(Long prodId) {
		log.info("get products " + prodId + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		EBProduct prod = null;
		try {
			tx.begin();
			prod = pm.getObjectById(EBProduct.class, prodId);

			tx.commit();
			log.finest("get products  success.");
		} catch (Exception e) {
			log.info("get products failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return prod;
	}

	public List<EBProduct> getProds() {
		log.info("get default products begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBProduct> prods = null;
		try {
			tx.begin();
			Query query = pm.newQuery(EBProduct.class);
			query.setRange(0, 10);
			prods = (List<EBProduct>) query.execute();
			tx.commit();
			log.finest("get products  success.");
		} catch (Exception e) {
			log.info("get products failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return prods;
	}

	public List<EBProduct> getProdsByFreightPriceScope(Float low, Float high) {
		log.info("get Prods By Freight Price Scope from " + low + " to " + high
				+ " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBProduct> prods = null;
		try {
			tx.begin();
			Query query = pm.newQuery(EBProduct.class);
			query.setFilter("freight >= '" + low + "'");
			query.setFilter("freight <= '" + high + "'");
			prods = (List<EBProduct>) query.execute();
			tx.commit();
			log.finest("get Prods By Freight Price Scope from " + low + " to "
					+ high + "   success.");
		} catch (Exception e) {
			log.info("get Prods By Freight Price Scope from " + low + " to "
					+ high + "  failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return prods;
	}

	public List<EBProduct> getProdsByFilter(String filter) {
		log.info("get Prods By Filter begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBProduct> prods = null;
		try {
			tx.begin();
			Query query = pm.newQuery(EBProduct.class);
			if (filter.equals("")) {
			} else {
				query.setFilter(filter);
			}
			prods = (List<EBProduct>) query.execute();
			tx.commit();
			log.finest("get Prods By Filter success.");
		} catch (Exception e) {
			log.info("get Prods By Filter failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return prods;
	}

	public List<EBProduct> getByProdsId(List<Long> prodsId) {
		log.info("get products by prodIds " + prodsId + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBProduct> prods = new ArrayList<EBProduct>();
		try {
			for (Long id : prodsId) {
				tx.begin();
				EBProduct p= pm.getObjectById(EBProduct.class, id);
				prods.add(p);
				tx.commit();
			}
			
			log.finest("get products by prodIds  success.");
		} catch (Exception e) {
			log.info("get products by prodIds failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return prods;
	}
}
